{% extends "shuup/front/macros/theme/general.jinja" %}

{% macro alert(message, title=None, mode="") %}
    <div class="alert{% if mode %} alert-{{ mode }}{% endif %}">
        {% if title %}
            <strong>{{ title }}</strong>
        {% endif %}
        {% if not caller %}
            {{ message|safe }}
        {% else %}
            {{ caller() }}
        {% endif %}
    </div>
{% endmacro %}

{% macro breadcrumb() %}
    <ol class="breadcrumb">
        {{ caller() }}
    </ol>
{% endmacro %}

{% macro render_breadcrumb_item(url, name, active=False) %}
    <li{% if active %} class="active"{% endif %}>
        {% if url %}<a href="{{ url }}">{{ name }}</a>{% else %}{{ name }}{% endif %}
    </li>
{% endmacro %}

{% macro render_field(field, classes="", layout="", show_help_text=True, render_label=True) %}
    {% if field.field.widget.is_hidden %}
        {{ field.as_widget()|safe }}
    {% else %}
        {% set widget_name = field.field.widget.__class__.__name__|lower %}
        {% if widget_name == "checkboxinput" %}
            <div class="custom-checkbox {{ widget_name }}{% if field.field.required %} required{% endif %}{% if field.errors %} has-error{% endif %} {{ classes }}">
                {# Force required=False for hidden fields, otherwise it crashes in browser while trying to focus it #}
                {{ (field|replace_field_attrs(required=false)).as_widget()|safe }}
                {% if render_label %}
                <label class="control-label" for="{{ field.auto_id }}">
                    {{ field.label }}
                </label>
                {% endif %}
                {% if field.errors %}
                    <p class="help-block">
                        {% for error in field.errors %}
                            {{ error}}
                        {% endfor %}
                    </p>
                {% endif %}
                {% if field.help_text and show_help_text %}
                    <p class="help-block">{{ field.help_text|safe }}</p>
                {% endif %}
            </div>

        {% elif widget_name == "checkboxselectmultiple" %}
            <div class="form-group {{ widget_name }}{% if field.field.required %} required{% endif %}{% if field.errors %} has-error{% endif %} {{ classes }}">
                {% if render_label %}
                <label class="control-label">{{ field.label }}</label>
                {% endif %}
                {% for subwidget in field.subwidgets %}
                    {% set option = subwidget.data %}
                    <div class="custom-checkbox">
                        <input type="{{ option.type }}" value="{{ option.value }}" name="{{ option.name }}" {% if option.attrs.checked %}checked{% endif %} id="{{ option.attrs.id }}">
                        <label for="{{ option.attrs.id }}">
                            {{ option.label }}
                        </label>
                    </div>
                {% endfor %}
                {% if field.help_text and show_help_text %}
                    <p class="help-block">{{ field.help_text }}</p>
                {% endif %}
                {% if field.errors %}
                    <p class="help-block">
                        {% for error in field.errors %}
                            {{ error}}
                        {% endfor %}
                    </p>
                {% endif %}
            </div>

        {% elif widget_name == "radio" %}
            <div class="custom-radio {{ widget_name }}{% if field.field.required %} required{% endif %}{% if field.errors %} has-error{% endif %} {{ classes }}">
                {{ field.as_widget()|safe }}
                {% if render_label %}
                <label class="control-label" for="{{ field.auto_id }}">
                    {{ field.label }}
                </label>
                {% endif %}
            </div>
        {% elif widget_name == "select" %}
            <div class="form-group {{ widget_name }}{% if field.field.required %} required{% endif %}{% if field.errors %} has-error{% endif %} {{ classes }}">
                {% if render_label %}
                <label class="control-label {% if layout == "horizontal" %}col-md-3{% endif %}" for="{{ field.auto_id }}">{{ field.label }}</label>
                {% endif %}
                {% if layout == "horizontal" %}
                    <div class="col-md-9">
                {% endif %}
                {# Force required=False for hidden fields, otherwise it crashes in browser while trying to focus it #}
                {{ (field|replace_field_attrs(required=false)).as_widget(attrs={"class": "form-control selectpicker"})|safe }}
                {% if field.help_text and show_help_text %}
                    <p class="help-block">{{ field.help_text }}</p>
                {% endif %}
                {% if field.errors %}
                    <p class="help-block">
                        {% for error in field.errors %}
                            {{ error}}
                        {% endfor %}
                    </p>
                {% endif %}
                {% if layout == "horizontal" %}
                    </div>
                {% endif %}
            </div>
        {% else %}
            <div class="form-group {{ widget_name }}{% if field.field.required %} required{% endif %}{% if field.errors %} has-error{% endif %} {{ classes }}">
                {% if render_label %}
                <label class="control-label {% if layout == "horizontal" %}col-md-3{% endif %}" for="{{ field.auto_id }}">{{ field.label }}</label>
                {% endif %}
                {% if layout == "horizontal" %}
                    <div class="col-md-9">
                {% endif %}
                {{ field.as_widget(attrs={"class": "form-control"})|safe }}
                {% if field.help_text and show_help_text %}
                    <p class="help-block">{{ field.help_text|safe }}</p>
                {% endif %}
                {% if field.errors %}
                    <p class="help-block">
                        {% for error in field.errors %}
                            {{ error}}
                        {% endfor %}
                    </p>
                {% endif %}
                {% if layout == "horizontal" %}
                    </div>
                {% endif %}
            </div>
        {% endif %}
    {% endif %}
{% endmacro %}

{% macro render_pagination(page, paginator, page_range=None, show_details=True) %}
    <script>
        window.PAGE_NUMBER = "{{ page.number }}";
    </script>
    <nav class="text-center">
        <ul class="pagination">
            {% if page.has_previous() %}
                <li id="previous_page">
                    <a href="#"
                       onclick="refreshFilters({{ page.previous_page_number()}}); return false;"
                       aria-label="{% trans %}Previous{% endtrans %}">
                        <i class="fa fa-angle-left"></i>
                    </a>
                </li>
            {% else %}
                <li id="previous_page" class="disabled">
                    <span aria-label="{% trans %}Previous{% endtrans %}">
                        <i class="fa fa-angle-left" aria-hidden="true"></i>
                    </span>
                </li>
            {% endif %}
            {% for i in page_range or paginator.page_range %}
                <li id="pagination_page_{{ i }}" class="{% if i == page.number %}active{% endif %}">
                    <a href="#" onclick="refreshFilters({{ i }}); return false;">{{ i }}</a>
                </li>
            {% endfor %}
            {% if page.has_next() %}
                <li id="next_page">
                    <a href="#"
                       onclick="refreshFilters({{ page.next_page_number() }}); return false;"
                       aria-label="{% trans %}Next{% endtrans %}">
                        <i class="fa fa-angle-right"></i>
                    </a>
                </li>
            {% else %}
                <li id="next_page" class="disabled">
                    <span aria-label="{% trans %}Next{% endtrans %}">
                        <i class="fa fa-angle-right" aria-hidden="true"></i>
                    </span>
                </li>
            {% endif %}
        </ul>
        {% if show_details %}
            <p class="text-muted">
                {{ gettext("Items %(start_index)d of %(end_index)d of %(count)d total", start_index=page.start_index(), end_index=page.end_index(), count=paginator.count) }}
            <p>
        {% endif %}
    </nav>
{% endmacro %}

{% macro render_maintenance_mode_notification() %}
    {{ render_admin_tools() }}
{% endmacro %}

{% macro render_toggle_all_seeing_list_item(is_all_seeing) %}
    <li>
        <a href="{{ url("shuup:toggle-all-seeing") }}">
            {% trans %}Change visibility limit to{% endtrans %}&nbsp;
            {% if is_all_seeing %}
                {% trans %}show only visible products and categories{% endtrans %}
            {% else %}
                {% trans %}show all products and categories{% endtrans %}
            {% endif %}
        </a>
    </li>
{% endmacro %}

{% macro render_customer_mode_change_from_anonymous() %}
    <li>
        <a href="{{ url("shuup:force-person-contact") }}">
            {% trans %}Change customer to person contact mode{% endtrans %}
        </a>
    </li>
    <li>
        <a href="{{ url("shuup:force-company-contact") }}">
            {% trans %}Change customer to company contact mode{% endtrans %}
        </a>
    </li>
{% endmacro %}

{% macro render_customer_mode_change_from_company() %}
    <li>
        <a href="{{ url("shuup:force-anonymous-contact") }}">
            {% trans %}Change customer mode to guest{% endtrans %}
        </a>
    </li>
    <li>
        <a href="{{ url("shuup:force-person-contact") }}">
            {% trans %}Change customer to person contact mode{% endtrans %}
        </a>
    </li>
{% endmacro %}

{% macro render_customer_mode_change_from_person() %}
    <li>
        <a href="{{ url("shuup:force-anonymous-contact") }}">
            {% trans %}Change customer mode to guest{% endtrans %}
        </a>
    </li>
    <li>
        <a href="{{ url("shuup:force-company-contact") }}">
            {% trans %}Change customer to company contact mode{% endtrans %}
        </a>
    </li>
{% endmacro %}

{% macro render_admin_tools() %}
    {% if shuup.general.is_shop_admin() %}
    {%- set is_maintenance_mode = request.shop.maintenance_mode -%}
    {%- set can_toggle_all_seeing = shuup.general.can_toggle_all_seeing() %}
    {%- set is_all_seeing = request.customer.is_all_seeing -%}
    {%- set is_anonymous = request.person.is_anonymous -%}
    {%- set is_company = request.is_company_member -%}
    <nav class="navbar navbar-default navbar-admin-tools navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <div class="navbar-brand">
                    <h2>{% trans %}Tools{% endtrans %}</h2>
                    <div class="admin-tools-info">
                    {% if is_maintenance_mode %}
                        <span class="badge badge-warning">{% trans %}Maintenance{% endtrans %}</span>
                    {% else %}
                        <span class="badge badge-success">{% trans %}Live{% endtrans %}</span>
                    {% endif %}
                    </div>
                    <div class="admin-tools-info">
                    {% if is_anonymous %}
                        <i class="fa fa-user-secret" title="{% trans %}Customer mode{% endtrans %}: {% trans %}GUEST{% endtrans %}"></i>
                    {% elif is_company %}
                        <i class="fa fa-globe" title="{% trans %}Customer mode{% endtrans %}: {% trans %}COMPANY CONTACT{% endtrans %}"></i>
                    {% else %}
                        <i class="fa fa-users" title="{% trans %}Customer mode{% endtrans %}: {% trans %}PERSON CONTACT{% endtrans %}"></i>
                    {% endif %}
                    </div>
                    {% if can_toggle_all_seeing %}
                        <div class="admin-tools-info">
                        {% if is_all_seeing %}
                            <i class="fa fa-eye" title="{% trans %}show all products and categories{% endtrans %}"></i>
                        {% else %}
                            <i class="fa fa-eye-slash" title="{% trans %}show only visible products and categories{% endtrans %}"></i>
                        {% endif %}
                        </div>
                    {% endif %}
                </div>
                <button type="button"
                    class="navbar-toggle collapsed"
                    data-toggle="collapse"
                    data-target="#admin-tools-menu"
                    aria-expanded="false">
                <span class="sr-only">{% trans %}Toggle navigation{% endtrans %}</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            </div>
            <div class="collapse navbar-collapse" id="admin-tools-menu">
                <ul class="nav navbar-nav navbar-right">
                {% if object %}
                    {% set edit_object_url = shuup.general.get_admin_edit_url(object) %}
                    {% if edit_object_url %}
                        <li class="nav-button">
                            <a
                                href="{{ edit_object_url.url }}"
                                target="_blank"
                                title="{{ _("Edit '{object_title}' in administration page").format(object_title=object) }}"
                            >
                                {{ _("Edit {object_name} in Admin").format(object_name=edit_object_url.name) }}
                            </a>
                        </li>
                    {% endif %}
                {% endif %}
                    <li>
                        <a href="{{ url("shuup_admin:dashboard") }}" target="_blank">{% trans %}Shop Administration{% endtrans %}</a>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle"
                           data-toggle="dropdown"
                           role="button"
                           aria-haspopup="true"
                           aria-expanded="false">{% trans %}Tools{% endtrans %} <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            {% if can_toggle_all_seeing %}
                                {{ render_toggle_all_seeing_list_item(is_all_seeing) }}
                            {% endif %}
                            {% if is_anonymous %}
                                {{ render_customer_mode_change_from_anonymous() }}
                            {% elif is_company %}
                                {{ render_customer_mode_change_from_company() }}
                            {% else %}
                                {{ render_customer_mode_change_from_person() }}
                            {% endif %}
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    {% endif %}
{% endmacro %}

{% macro render_messages() %}
    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-dismissible alert-{{ message.tags }}" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                {{ message }}
            </div>
        {% endfor %}
    {% endif %}
{% endmacro %}

{% macro render_errors() %}
    {% if form %}
        {% if form.non_field_errors and form.non_field_errors() %}
            {% for error in form.non_field_errors() %}
            <div class="alert alert-dismissible alert-danger" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <i class="fa fa-exclamation-circle"></i> {{ error }}
            </div>
            {% endfor %}
        {% elif form.errors %}
            <div class="alert alert-dismissible alert-danger" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <i class="fa fa-exclamation-circle"></i> {% trans %}There were errors on submitted form fields. Please check them and try again.{% endtrans %}
            </div>
        {% endif %}
    {% endif %}
{% endmacro %}

{% macro render_metadata(object, title, text, content_type) %}
    {% if text %}
        <meta name="description" content="{{ text|safe|striptags|truncate(160)|replace("\n", " ")|replace("\"", "'") }}">
    {% endif %}
    {{ render_open_graph_metadata(object, title, text, content_type) }}
{% endmacro %}

{% macro render_open_graph_metadata(object, title, text, content_type) %}
    <meta property="og:site_name" content="{{ request.shop }}">
    <meta property="og:type" content="{{ content_type }}">
    <meta property="og:url" content="{{ shuup.urls.model_url(object, absolute=True) }}">
    <meta property="og:title" content="{{ title }}">
    {% if text %}
        <meta property="og:description" content="{{ text|safe|striptags|truncate(160)|replace("\n", " ")|replace("\"", "'") }}">
    {% endif %}
    {% if content_type == "product" %}
        {% set object_image = object.primary_image or object.media.filter(shops=request.shop, kind=2).order_by("ordering").first() %}
    {% else %}
        {% set object_image = object.image %}
    {% endif %}
    {% if object_image %}
        {% set thumbnail = object_image|thumbnail(size=(850, 850), crop="smart", upscale=False) %}
        <meta property="og:image" content="{{ request.build_absolute_uri(thumbnail) }}">
        <meta property="og:image:width" content="850" />
        <meta property="og:image:height" content="850" />
    {% endif %}
{% endmacro %}

{% macro render_supplier_info(supplier, render_link=True) %}
    {% set supplier_url = shuup.urls.model_url(supplier, raise_when_not_found=False) %}
    {% if supplier_url and render_link %}<a class="supplier-info-link" href="{{ supplier_url }}">{% endif %}
        <p class="supplier-info"><small>{{ _("By <strong>%(supplier_name)s</strong>", supplier_name=supplier.name) }}</small></p>
    {% if supplier_url and render_link %}</a>{% endif %}
{% endmacro %}

{% macro render_availability_info(shop_product) %}
    <div class="available-until">
        <small>{{ _('Available until: <span class="availability-date">{date}</span></small>').format(date=shop_product.available_until|date) }}
    </div>
{% endmacro %}
